<script setup>
import { ref, onMounted } from "vue";
import { queryPageApi } from "@/api/user";
import { queryListApi as queryDeptListApi } from "@/api/dept";
import { ElMessage } from "element-plus";
import { Search, Refresh, Plus, Edit, Delete } from "@element-plus/icons-vue";

//刷新
onMounted(() => {
  getDeptList();
  search();
});

// 查询参数
const queryParams = ref({
  name: "",
  phone: "",
  deptId: "",
  status: "",
  page: 1,
  pageSize: 10,
});

// 表格数据
const userList = ref([]);
const total = ref(0);

// 部门列表
const deptList = ref([]);

// 获取部门列表
const getDeptList = async () => {
  const result = await queryDeptListApi();
  if (result.code == 1) {
    deptList.value = result.data;
  }
};

// 搜索
const search = async () => {
  const result = await queryPageApi(queryParams.value);
  if (result.code == 1) {
    userList.value = result.data.rows;
    total.value = result.data.total;
  }
};

// 清空
const clear = () => {
  queryParams.value = {
    name: "",
    phone: "",
    deptId: "",
    status: "",
    page: 1,
    pageSize: 10,
  };
  search();
};

// 分页大小改变
const handleSizeChange = (val) => {
  search();
};

// 页码改变
const handleCurrentChange = (val) => {
  search();
};

// 性别显示
const getGenderText = (gender) => {
  return gender === 1 ? "男" : "女";
};

// 状态显示
const getStatusText = (status) => {
  return status === 1 ? "正常" : "停用";
};

// 添加用户（占位符）
const handleAdd = () => {
  // TODO: 实现添加用户功能
  ElMessage.info("添加用户功能待实现");
};

// 编辑用户（占位符）
const handleEdit = (id) => {
  // TODO: 实现编辑用户功能
  ElMessage.info("编辑用户功能待实现");
};

// 删除用户（占位符）
const handleDelete = (id) => {
  // TODO: 实现删除用户功能
  ElMessage.info("删除用户功能待实现");
};

// 批量删除（占位符）
const handleBatchDelete = () => {
  // TODO: 实现批量删除功能
  ElMessage.info("批量删除功能待实现");
};
</script>

<template>
  <div class="user-management">
    <!-- 搜索区域 -->
    <el-form
      :inline="true"
      :model="queryParams"
      ref="queryForm"
      class="search-form"
    >
      <el-form-item label="姓名">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机号">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="部门">
        <el-select
          v-model="queryParams.deptId"
          placeholder="请选择"
          clearable
          style="width: 100%"
        >
          <el-option
            v-for="dept in deptList"
            :key="dept.id"
            :label="dept.name"
            :value="dept.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 100%"
        >
          <el-option label="正常" :value="1" />
          <el-option label="停用" :value="0" />
        </el-select>
      </el-form-item>
      <el-form-item class="search-buttons">
        <el-button type="primary" @click="search"
          ><el-icon><Search /></el-icon>搜索</el-button
        >
        <el-button @click="clear"
          ><el-icon><Refresh /></el-icon>清空</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <div class="operation-area">
      <el-button type="primary" @click="handleAdd"
        ><el-icon><Plus /></el-icon>添加用户</el-button
      >
      <el-button type="danger" @click="handleBatchDelete"
        ><el-icon><Delete /></el-icon>批量删除</el-button
      >
    </div>

    <!-- 表格区域 -->
    <el-table :data="userList" style="width: 100%" border>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="name" label="姓名" width="120" align="center" />
      <el-table-column
        prop="username"
        label="用户名"
        width="160"
        align="center"
      />
      <el-table-column label="头像" width="80" align="center">
        <template #default="scope">
          <el-avatar :size="40" :src="scope.row.image" :alt="scope.row.name" />
        </template>
      </el-table-column>
      <el-table-column
        prop="deptName"
        label="部门"
        width="160"
        align="center"
      />
      <el-table-column
        prop="roleName"
        label="角色"
        width="150"
        align="center"
      />
      <el-table-column prop="phone" label="手机号" width="160" align="center" />
      <el-table-column label="状态" width="100" align="center">
        <template #default="scope">
          {{ getStatusText(scope.row.status) }}
        </template>
      </el-table-column>
      <el-table-column
        prop="updateTime"
        label="最后操作时间"
        width="180"
        align="center"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button type="primary" link @click="handleEdit(scope.row.id)">
            <el-icon><Edit /></el-icon>修改
          </el-button>
          <el-button type="danger" link @click="handleDelete(scope.row.id)">
            <el-icon><Delete /></el-icon>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.page"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40, 50]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped>
.user-management {
  padding: 20px;
  background-color: #fff;
  min-height: 100%;
  border-radius: 8px; /* 添加圆角 */
}

.operation-area {
  margin: 20px 0;
}

.search-form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.search-form :deep(.el-form-item) {
  margin-right: 20px;
  margin-bottom: 20px;
  min-width: 200px;
}

.search-buttons {
  margin-left: auto !important;
  min-width: auto !important;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

:deep(.el-button .el-icon) {
  margin-right: 4px;
}
</style>
